package org.fastiot.data.archives

import grails.converters.JSON
import grails.testing.gorm.DomainUnitTest
import org.fastiot.data.DataSet
import org.grails.web.converters.configuration.ConvertersConfigurationHolder
import org.grails.web.converters.configuration.DefaultConverterConfiguration
import org.grails.web.converters.marshaller.json.CollectionMarshaller
import org.grails.web.converters.marshaller.json.GenericJavaBeanMarshaller
import org.grails.web.converters.marshaller.json.MapMarshaller
import spock.lang.Specification

class TrendSpec extends Specification implements DomainUnitTest<Trend> {

    def setupSpec() {
        def defaultConverterConfig = new  DefaultConverterConfiguration<JSON>()
        defaultConverterConfig.registerObjectMarshaller(new MapMarshaller())
        defaultConverterConfig.registerObjectMarshaller(new CollectionMarshaller())
        defaultConverterConfig.registerObjectMarshaller(new GenericJavaBeanMarshaller())

        ConvertersConfigurationHolder.setDefaultConfiguration(JSON.class, defaultConverterConfig)
    }

    def setup() {
    }

    def cleanup() {
    }

    void "Test a valid properties"() {
        given:
        def instance = new Trend(
                terminalCode: terminalCode, dateStart: dateStart, dateEnd: dateEnd,
                dataSet: dataSet, metadata: metadata, contents: contents
        )

        when:
        instance.save(flush: true)

        then:
        !instance.hasErrors()

        where:
        terminalCode << ["LN1234567890123456"]
        dateStart << [new GregorianCalendar(2018, 2, 14, 12, 0, 0).time]
        dateEnd << [new GregorianCalendar(2018, 2, 14, 12, 59, 59).time]
        dataSet << [
                new DataSet(code: "ds1", name: "数据集1", scene: DataSet.AggregateScene.TimeSharing).save(flush: true)
        ]
        metadata << [
                ([
                        [code: 'SOC', name: "荷电状态"], [code: 'KPH', name: "时速"], [code: 'LAT', name: "纬度"],
                        [code: 'LON', name: "经度"], [code: 'TEMP', name: "温度"], [code: 'VIB', name: "振动"],
                        [code: 'WARN', name: "报警", enumRange: [0: "正常", 1: "故障"]]
                ] as JSON) as String
        ]
        contents << [
                ([
                        20180214121304: [80, 37.5, 39.5427, 116.2317, 80.3, 10.5, 0],
                        20180214121314: [78, 0, 39.5427, 116.2317, 80.3, null, 1],
                        20180214121324: [76, 10.2, null, null, 80.3, 1.1, 0]
                ] as JSON) as String
        ]
    }
}
